<?php
/*
 * @Created by: wangchengcheng
 * @Author: wangchengcheng
 * @Date: 2020-01-08 17:51:30
 * @LastEditors  : wangchengcheng
 * @LastEditTime : 2020-01-09 11:22:58
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Site\Store;
use App\Http\Requests\Site\Update;
use Illuminate\Http\Request;
use App\Models\Site;
use Cache;

class SiteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $site = Site::orderBy('sort')
            ->withTrashed()
            ->get();
        $assign = compact('site');

        return view('admin.site.index', $assign);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.site.create');
    }

    /**
     * Store a newly created resource in storage.
     * 
     * @param $request
     * 
     * @return Response
     */
    public function store(Store $request)
    {
        $data = $request->except('_token', 's');
        if(empty($data['sort'])){
            //获取序列号
            $sort = Site::orderBy('sort', 'desc')->value('sort');
            $data['sort'] = (int) $sort + 1;
        }
        Site::create($data);

        return redirect('admin/site/index');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $site   = Site::withTrashed()->find($id);
        $assign = compact('site');
        
        return view('admin.site.edit', $assign);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Update $request, $id)
    {
        Site::withTrashed()->find($id)->update($request->except('_token', 's'));

        return redirect()->back();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function ajaxUpdate(Request $request, $id)
    {
        Site::withTrashed()->find($id)->update($request->except('_token', 's'));

        return redirect()->back();
    }
    
    /**
     * 排序
     * 
     * @return RedirectResponse
     */
    public function sort(Request $request, Site $siteModel)
    {
        $data = $request->except('_token', 's');
        $editData = [];
        foreach($data as $k => $v){
            $editData[] = [
                'id' => $k,
                'sort' => $v,
            ];
        }

        $result = $siteModel->updateBatch($editData);

        if($result){
            //更新缓存
            Cache::forget('home:site');
        }

        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //dd($id);
        Site::destroy($id);

        return redirect()->back();
    }

    /**
     * 恢复删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function restore($id)
    {
        //dd($id);
        Site::onlyTrashed()->find($id)->restore();

        return redirect('admin/site/index');
    }

    /**
     * 彻底删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function forceDelete($id)
    {
        Site::onlyTrashed()->find($id)->forceDelete();

        return redirect('admin/site/index');
    }
}
